package com.sls.game.service.impl;

import java.util.UUID;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.ServerDisponseException;
import com.sls.core.util.ServerDateAndTime;
import com.sls.game.constant.ExceptionCodeDes;
import com.sls.game.dao.EnlinkProductAreaServerDAO;
import com.sls.game.page.Pagination;
import com.sls.game.pojo.enlink.EnlinkProductAreaServer;
import com.sls.game.service.EnLinkProductAreaServerService;

@Service("enLinkProductAreaServerService")
public class EnLinkProductAreaServerServiceImpl implements
		EnLinkProductAreaServerService {

	private static Logger log = Logger
			.getLogger(EnLinkProductAreaServerServiceImpl.class);
	@Resource(name = "enLinkProductAreaServerDAO")
	private EnlinkProductAreaServerDAO enlinkAreaServerDAO;
	private Gson gson;

	public EnLinkProductAreaServerServiceImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public void save(String body, Head head) throws ServerDisponseException {
		EnlinkProductAreaServer enlink = gson.fromJson(body,
				EnlinkProductAreaServer.class);
		this.validate(enlink);
		this.validateField(enlink);
		try {
			enlink.setId(UUID.randomUUID().toString());
			String insertTime = ServerDateAndTime.getNowDateTimeOfFormat();
			enlink.setInsertTime(insertTime);

			this.enlinkAreaServerDAO.insert(enlink);
		} catch (Exception e) {
			log.error(e);
			throw new ServerDisponseException(
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
		}
	}

	@Override
	public Pagination<EnlinkProductAreaServer> queryEnLinkAreaServer(
			String body, Head head) throws ServerDisponseException {
		EnlinkProductAreaServer enlink = gson.fromJson(body,
				EnlinkProductAreaServer.class);
		this.validate(enlink);
		try {
			Pagination<EnlinkProductAreaServer> page = this.enlinkAreaServerDAO
					.queryPaginationList(enlink);
			return page;
		} catch (Exception e) {
			log.error(e);
			throw new ServerDisponseException(
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
		}
	}

	/**
	 * 校验是否为null
	 * 
	 * @param obj
	 * @throws ServerDisponseException
	 */
	private void validate(Object obj) throws ServerDisponseException {
		if (obj == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.PARSE_REQUEST_ERROR.getCode(),
					ExceptionCodeDes.PARSE_REQUEST_ERROR.getDescription());
		}
	}

	private void validateField(EnlinkProductAreaServer as)
			throws ServerDisponseException {
		if (StringUtils.isEmpty(as.getProductId())) {
			throw new ServerDisponseException(
					ExceptionCodeDes.PRODCODE_EMPTY.getCode(),
					ExceptionCodeDes.PRODCODE_EMPTY.getDescription());
		} else if (StringUtils.isEmpty(as.getBigAreaName())) {
			throw new ServerDisponseException(
					ExceptionCodeDes.BIG_AREA_NAME_EMPTY.getCode(),
					ExceptionCodeDes.BIG_AREA_NAME_EMPTY.getDescription());
		}

	}

	/**
	 * 清空区服表
	 * 
	 * @param body
	 * @param head
	 * @return
	 * @throws ServerDisponseException
	 */
	@Override
	public String deleteAreaServer(String body, Head head)
			throws ServerDisponseException {
		try {
			this.enlinkAreaServerDAO.delete();
		} catch (Exception e) {
			log.error(e);
			throw new ServerDisponseException(
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
		}

		return null;
	}

}
